// 2023-04-11 15:47:37  事件模块
// 注意同一个event注册同名事件问题
import { EventEmitter } from 'events'
/**
 * @type HuiLY_EventEmitter
 */
let huily_event = null

/**
 * @augments EventEmitter
 */
class HuiLY_EventEmitter extends EventEmitter {
    constructor(options) {
        super(options)
    }
    /**
     * @param {String} eventName 
     * @param {Function} callback
     * @returns {Boolean} true:绑定成功 fales:绑定失败
     */
    AddListerEvent(eventName, callback) {
        try {
            if (this.eventNames().includes(eventName)) {
                throw new Error(`重复绑定${eventName}事件`)
            }
            this.addListener(eventName, callback)
            return true
        } catch (error) {
            console.error(error);
            return false
        }
    }
}
/**
 * @param {Boolean | Undefined}  options
 * @returns {HuiLY_EventEmitter}
 * 返回唯一event实例
 */
function GetOnlyEvent(options = undefined) {
    huily_event = huily_event !== null ? huily_event : new HuiLY_EventEmitter(options)
    return huily_event
}

export default GetOnlyEvent
